package com.ms.seguridad.jsf;

import com.ms.seguridad.service.SecurityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;


/**
 * User: AW
 * Date: 24/02/13
 */
@ManagedBean
@Scope("request")
@Component
public class LoginBean {
    private String user = "";
    private String password = "";
    private String oldPassword = "";
    private String newPassword = "";
    private String confirmNewPassword = "";
    private boolean storeUser = false;
    private boolean logIn = false;
    private static final Logger LOG = LoggerFactory.getLogger(LoginBean.class);
    private Long selectedPortalId;

    @Autowired
    protected SecurityService securityService;

    @PostConstruct
    public void init() {

    }

    public String loginAction() throws IOException, ServletException {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        ServletRequest request = ((ServletRequest) context.getRequest());
        RequestDispatcher dispatcher = request.getRequestDispatcher("/j_spring_security_check?j_portal=" + selectedPortalId);
        dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());
        FacesContext.getCurrentInstance().responseComplete();
        return null;
    }


    public String getUser() {
        return this.user;
    }

    public void setUser(final String user) {
        this.user = user;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(final String password) {
        this.password = password;
    }

    public boolean isStoreUser() {
        return this.storeUser;
    }

    public void setStoreUser(final boolean storeUser) {
        this.storeUser = storeUser;
    }

    public boolean isLogIn() {
        return this.logIn;
    }

    public void setLogIn(final boolean logIn) {
        this.logIn = logIn;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getConfirmNewPassword() {
        return confirmNewPassword;
    }

    public void setConfirmNewPassword(String confirmNewPassword) {
        this.confirmNewPassword = confirmNewPassword;
    }


    public Long getSelectedPortalId() {
        return selectedPortalId;
    }

    public void setSelectedPortalId(Long selectedPortalId) {
        this.selectedPortalId = selectedPortalId;
    }
}
